package com.wanmait.spacesys.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mysql.cj.Session;
import com.wanmait.spacesys.dao.MyCollectionDAO;
import com.wanmait.spacesys.dao.UpdateUserinfoCenterDAO;
import com.wanmait.spacesys.vo.Collection;
import com.wanmait.spacesys.vo.Userinfo;


@WebServlet("/MyCollectionController")
public class MyCollectionController extends BaseController {

	protected void getMyCollection(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String pageNumStr=request.getParameter("pageNum");
		int pageNum=1;
		try {
			pageNum=Integer.parseInt(pageNumStr);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			/* e.printStackTrace(); */
		}
		Userinfo userinfo = (Userinfo) request.getSession().getAttribute("userinfo");
		if (userinfo != null) {
			
			
			int count=MyCollectionDAO.getMyCollectionDAO().getCount(userinfo.getId());
			int pageCount=(count+1)/2;//总页数
			//pageNum当前页   (最后一页)
			if(pageNum>pageCount) {
				pageNum=pageCount;
			}
			
			
           List<Collection> collections=MyCollectionDAO.getMyCollectionDAO().findAllById(userinfo.getId(),pageNum);
           request.getSession().setAttribute("collections", collections);
           request.setAttribute("count", count);
			/* System.out.println(count); */
           this.forward("/web/user/collection.jsp", request, response);
		}
	}
	
	
	
		//用户个人中心收藏内容详情页面的显示
		protected void contentDisplay(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			Userinfo userinfo = (Userinfo) request.getSession().getAttribute("userinfo");
			String idStr = request.getParameter("id");
			Integer id = Integer.parseInt(idStr);
			if (userinfo == null) {
				this.redirect("/web/user/collection.jsp?msg=error", request, response);
			}else {
				
				int count=MyCollectionDAO.getMyCollectionDAO().getCount(userinfo.getId());
		           Collection collection=MyCollectionDAO.getMyCollectionDAO().contentDisplayFindAll(userinfo.getId(),id);
		           request.getSession().setAttribute("collection", collection);
		           request.setAttribute("count", count);
					/* System.out.println(count); */
		           this.forward("/web/user/contentDisplay.jsp", request, response);
			}
			
		}
	
	//ajax删除收藏
	protected void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String idStr = request.getParameter("id");
		Integer id = Integer.parseInt(idStr);
		MyCollectionDAO.getMyCollectionDAO().delete(id);
	}
	//用户个人中心:用户退出
	protected void removeUserinfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 HttpSession session=request.getSession();
	     session.removeAttribute("userinfo");
	     this.redirect("/web/index.jsp", request, response);
		
		}

	@Override
	protected void doDefault(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.getMyCollection(request, response);

	}

}
